function addClass(className,_t){
    _t.className = (_t.className + ' ' + className).replace(/\s{2,}/g,' ').replace(/^\s+|\s+$/g,'');
}
function removeClass(className,_t){
    _t.className = _t.className.replace(new RegExp('(^|\\s)' + className + '(?:\\s|$)'), '$1').replace(/\s{2,}/g,' ').replace(/^\s+|\s+$/g,'');
}
function toggleView(pre_id,_t){
    var p = document.getElementById("pre_"+pre_id);
    if(_t.st == "hidden"){
        // wrap
        p.style["overflow"] = "hidden";
        addClass("text-wrap",p);
        _t.innerHTML = "s";
        _t.st = "wrap"
    }else if(_t.st == "wrap"){
        // scroll
        p.style["overflow"] = "scroll";
        removeClass("text-wrap",p);
        _t.innerHTML = "h";
        _t.st = "scroll"
    }else if(_t.st == "scroll"){
        // hidden
        p.style["overflow"] = "hidden";
        removeClass("text-wrap",p);
        _t.innerHTML = "w";
        _t.st = "hidden"
    }else{
        // wrap
        p.style["overflow"] = "hidden";
        addClass("text-wrap",p);
        _t.innerHTML = "s";
        _t.st = "wrap"
    }
    return false;
}
var jk = {
    init:function()
    {
        jk.txt = $("#txt");
        jk.post_list = $("ul.post-list");
        jk.btn = $("input.btn");
        jk.effect = {
            height:"toggle",
            opacity: 'toggle'
        };
        jk.initEvents();
    },
    initEvents:function()
    {
        jk.regPostEvents();
        jk.regSubmitEvent();
    },
    regPostEvents:function()
    {
        $(".paste-body").dblclick(function(){
            $(this).animate(jk.effect);
        });
        $(".paste-foot").dblclick(function(){
            $(this).prev().animate(jk.effect);
        });
    },
    post_begin:function()
    {
        jk.txt.addClass("loading");
        jk.txt.attr("disabled","disabled");
        jk.btn.addClass("dis");
        jk.btn.attr("disabled","disabled");
        
    },
    post_end:function()
    {
        jk.txt.removeAttr("disabled");
        jk.txt.removeClass("loading");
        jk.btn.removeAttr("disabled");
        jk.btn.removeClass("dis");
    },
    regSubmitEvent:function()
    {
        $("input.btn").click(function(){
            var txt = jk.txt;
            text = txt.val();
            if(text.length==0)
            {
                return false;
            }
            jk.post_begin();
            $.ajax({
                type: "POST",
                url: "/ajax_add",
                data:({
                    txt : txt.val()
                }),
                complete : function(xhr){
                    if(xhr.status==200)
                    {
                        data = xhr.responseText;
                        if(data.length>0)
                        {
                            var e = $(data);
                            e.hide();
                            jk.post_list.prepend(e);
                            //e.slideDown();
                            e.animate(jk.effect, 500, function() {
                                jk.txt.focus();
                                jk.regPostEvents(); // list changed.re register list events.
                            });
                            jk.txt.val("");
                        }
                    }
                    else
                    {
                        alert(xhr.statusText);
                    }
                    jk.post_end();
                }
            });
            return false;
        });

    }
}

$(document).ready(function() {
    jk.init();
});

